package com.wxm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wxm.config.TlPayProperties;
import com.wxm.eum.PaySceneEnum;
import com.wxm.eum.TlPayTypeEnum;
import com.wxm.param.TlCancelParam;
import com.wxm.param.TlQueryParam;
import com.wxm.param.TlRefundParam;
import com.wxm.param.TlUnifiedPayParam;
import com.wxm.result.TlCancelResult;
import com.wxm.result.TlQueryResult;
import com.wxm.result.TlRefundResult;
import com.wxm.result.TlUnifiedPayResult;
import com.wxm.service.TongLianService;
import com.wxm.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 *      通联支付ServiceImpl
 * </p>
 *
 * @author: wxm
 * @Date: 2023/4/7
 */
@Slf4j
@Service
public class TongLianServiceImpl implements TongLianService, ParamJsonUtils {
    @Autowired
    private TlPayProperties tlPayProperties;

    /**
     * 获取统一支付信息
     * @param param 参数
     * @param paySceneEnum 支付场景
     * @return String
     */
    @Override
    public String unifiedPay(TlUnifiedPayParam param, PaySceneEnum paySceneEnum) {
        //判断支付场景
        if (PaySceneEnum.WX_BROWSER.equals(paySceneEnum) || PaySceneEnum.WX_SCAN.equals(paySceneEnum)){
            //H5-微信浏览器支付 H5-浏览器扫码支付
            param.setPayType(TlPayTypeEnum.WX_SCAN.getCode());
        }else if(PaySceneEnum.WX_JS.equals(paySceneEnum)){
            //微信-JS支付
            param.setPayType(TlPayTypeEnum.WX_JS.getCode());
        }else {
            //小程序微信支付
            param.setPayType(TlPayTypeEnum.WX_APP.getCode());
        }

        //手动校验参数值
        ValidatorUtils.validateEntity(param);
        JSONObject j = param.toJsonObject();
        //获取加签Sign
        String sign = TlUtils.createSign(j, tlPayProperties.getMchRsaPrivateKey(), TlUtils.getSignType(tlPayProperties.getSignType()));
        j.put("sign",sign);

        //发送请求
        TlUnifiedPayResult result = HttpUtils.postParamSend(tlPayProperties.getApiBaseUrl() + "/pay", j, new JSONObject(), TlUnifiedPayResult.class, (res,clazz) -> {
            TlUnifiedPayResult r = JSONObject.toJavaObject(JSONObject.parseObject(res), clazz);
            if ("FAIL".equals(r.getRetCode())){
                R.exception(r.getRetMsg());
            }
            return r;
        });
        //如果微信扫码需要转成Base64的二维码
        if (PaySceneEnum.WX_SCAN.equals(paySceneEnum)){
            try {
                return QRCodeUtil.encode(result.getPayinfo());
            }catch (Exception e){
                R.exception("支付码生成失败请重试");
            }
        }
        return result.getPayinfo();
    }

    /**
     * 统一撤销接口
     * @param param 参数
     * @return TlCancelResult
     */
    @Override
    public TlCancelResult cancel(TlCancelParam param) {
        //手动校验参数值
        ValidatorUtils.validateEntity(param);

        JSONObject j = param.toJsonObject();
        //获取加签Sign
        String sign = TlUtils.createSign(j, tlPayProperties.getMchRsaPrivateKey(), TlUtils.getSignType(tlPayProperties.getSignType()));
        j.put("sign",sign);

        //发送请求
        return HttpUtils.postParamSend(tlPayProperties.getApiBaseUrl() + "/cancel", j, new JSONObject(), TlCancelResult.class, (res,clazz) -> {
            TlCancelResult r = JSONObject.toJavaObject(JSONObject.parseObject(res), clazz);
            if ("FAIL".equals(r.getRetCode())){
                R.exception(r.getRetMsg());
            }
            return r;
        });
    }

    /**
     * 统一退款接口
     * @param param 参数
     * @return TlRefundResult
     */
    @Override
    public TlRefundResult refund(TlRefundParam param) {
        //手动校验参数值
        ValidatorUtils.validateEntity(param);

        JSONObject j = param.toJsonObject();
        //获取加签Sign
        String sign = TlUtils.createSign(j, tlPayProperties.getMchRsaPrivateKey(), TlUtils.getSignType(tlPayProperties.getSignType()));
        j.put("sign",sign);

        //发送请求
        return HttpUtils.postParamSend(tlPayProperties.getApiBaseUrl() + "/refund", j, new JSONObject(), TlRefundResult.class, (res,clazz) -> {
            TlRefundResult r = JSONObject.toJavaObject(JSONObject.parseObject(res), clazz);
            if ("FAIL".equals(r.getRetCode())){
                R.exception(r.getRetMsg());
            }
            return r;
        });
    }

    /**
     * 统一查询接口
     * @param param 参数
     * @return TlQueryResult
     */
    @Override
    public TlQueryResult query(TlQueryParam param) {
        //手动校验参数值
        ValidatorUtils.validateEntity(param);

        JSONObject j = param.toJsonObject();
        //获取加签Sign
        String sign = TlUtils.createSign(j, tlPayProperties.getMchRsaPrivateKey(), TlUtils.getSignType(tlPayProperties.getSignType()));
        j.put("sign",sign);

        //发送请求
        return HttpUtils.postParamSend(tlPayProperties.getApiBaseUrl() + "/query", j, new JSONObject(), TlQueryResult.class, (res,clazz) -> {
            TlQueryResult r = JSONObject.toJavaObject(JSONObject.parseObject(res), clazz);
            if ("FAIL".equals(r.getRetCode())){
                R.exception(r.getRetMsg());
            }
            return r;
        });
    }
}
